/**
 * 
 */
package com.jf.cloud.service.sysmanager.common.taglib.html;


import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import com.jf.cloud.service.sysmanager.constant.TagConstant;

/**
 * 复选框组标签处理类
 * 
 * @author 程伟平 2012-2-15
 */
public class CheckBoxGroupTag extends AbstractHtmlFormElementTag {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2323710691309959307L;

	/**
	 * 选项列表
	 */
	private List<Map<String, Object>> options;

	/**
	 * 选项键
	 */
	private String labelKey;

	/**
	 * 选项值键
	 */
	private String valueKey;

	/**
	 * 获取选项列表
	 * 
	 * @return 选项列表
	 */
	public List<Map<String, Object>> getOptions() {
		return options;
	}

	/**
	 * 设置选项列表
	 * 
	 * @param options 选项列表
	 */
	public void setOptions(List<Map<String, Object>> options) {
		this.options = options;
	}

	/**
	 * 获取选项键
	 * 
	 * @return 选项键
	 */
	public String getLabelKey() {
		return labelKey;
	}

	/**
	 * 设置选项键
	 * 
	 * @param labelKey 选项键
	 */
	public void setLabelKey(String labelKey) {
		this.labelKey = labelKey;
	}

	/**
	 * 获取选项值键
	 * 
	 * @return 选项值键
	 */
	public String getValueKey() {
		return valueKey;
	}

	/**
	 * 设置选项值键
	 * 
	 * @param valueKey 选项值键
	 */
	public void setValueKey(String valueKey) {
		this.valueKey = valueKey;
	}

	@Override
	public int doStartTag() throws JspException {
		if (name == null || name.length() == 0) {
			throw new JspException("复选框组标签名称属性――\"name\"不能为空！");
		}

		if (options == null || options.size() == 0) {
			throw new JspException("复选框组标签选项列表属性――\"options\"不能为空！");
		}

		JspWriter writer = pageContext.getOut();

		// 复选框标签
		StringBuffer sb = new StringBuffer();
		for (Map<String, Object> option : options) {
			String val = String.valueOf(option.get(TagConstant.DICT_OPTION_VALUE_KEY));
			if (valueKey != null && valueKey.length() > 0) {
				val = String.valueOf(option.get(valueKey));
			}

			sb.append("<input");
			if (name != null && name.length() > 0) {
				if (id != null && id.length() > 0) {
					sb.append(" id=\"");
					sb.append(id);
					sb.append("_");
					sb.append(val);
					sb.append("\"");
				} else {
					sb.append(" id=\"");
					sb.append(name);
					sb.append("_");
					sb.append(val);
					sb.append("\"");
				}

				sb.append(" name=\"");
				sb.append(name);
				sb.append("\"");
			}
			sb.append(" type=\"checkbox\"");
			sb.append(" value=\"");
			sb.append(val);
			sb.append("\"");
			if (value != null && value.length() > 0) {
				String[] vals = value.split(",");
				for (int i = 0; i < vals.length; i++) {
					if (vals[i] != null && vals[i].equals(val)) {
						sb.append(" checked=\"checked\"");
					}
				}
			}
			sb.append(" />");

			sb.append("<label");
			if (name != null && name.length() > 0) {
				sb.append(" for=\"");
				sb.append(name);
				sb.append("_");
				sb.append(val);
				sb.append("\"");
			}
			sb.append(">");
			if (labelKey != null && labelKey.length() > 0) {
				sb.append(option.get(labelKey));
			} else {
				sb.append(option.get(TagConstant.DICT_OPTION_LABEL_KEY));
			}
			sb.append("</label>\n");
		}

		try {
			writer.println(sb.toString());
		} catch (IOException ex) {
			throw new JspException("复选框组标签HTML输出异常！", ex);
		}

		return SKIP_BODY;
	}
}
